package com.kly.cashmall.module.splash;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.gyf.immersionbar.ImmersionBar;
import com.kly.cashmall.R;
import com.kly.cashmall.base.app_action.ActionHeads;
import com.kly.cashmall.base.app_action.BaseActionHelper;
import com.kly.cashmall.module.splash.adapter.GuideWelcomeAdapter;
import com.kly.cashmall.services.UserInfoHelper;
import com.tmall.ultraviewpager.UltraViewPager;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author
 */
public class GuideWelcomeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide_welcome);
        ImmersionBar.with(this)
                .statusBarDarkFont(true, 0.2f)
                .init();
        UltraViewPager ultraViewpagerGuide = findViewById(R.id.ultra_viewpager_guide);

        final TextView btnToMain = findViewById(R.id.btn_to_main);


        ultraViewpagerGuide.setScrollMode(UltraViewPager.ScrollMode.HORIZONTAL);

        final List<Integer> guideList = new ArrayList<>();
        guideList.add(R.mipmap.img_guide_first);
        guideList.add(R.mipmap.img_guide_second);

        PagerAdapter adapter = new GuideWelcomeAdapter(this, guideList,
                Arrays.asList(R.string.guide_title_one, R.string.guide_title_two), Arrays.asList(R.string.guide_content_one_one, R.string.guide_content_two_one), Arrays.asList(R.string.guide_content_one_two, R.string.guide_content_two_two), Arrays.asList(R.string.guide_content_one_three, R.string.guide_content_two_three));

        ultraViewpagerGuide.setAdapter(adapter);

        ultraViewpagerGuide.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == guideList.size() - 1) {
                    btnToMain.setVisibility(View.VISIBLE);
                } else {
                    btnToMain.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //set an infinite loop
        ultraViewpagerGuide.setInfiniteLoop(false);
        ultraViewpagerGuide.initIndicator();
        ultraViewpagerGuide.getIndicator()
                .setOrientation(UltraViewPager.Orientation.HORIZONTAL)
                .setFocusColor(getResources().getColor(R.color.color_FFBE07))
                .setNormalColor(getResources().getColor(R.color.color_D8D8D8))
                .setRadius((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 6, getResources().getDisplayMetrics()));
        ultraViewpagerGuide.getIndicator().setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
        ultraViewpagerGuide.getIndicator().build();
        btnToMain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserInfoHelper.getInstance().setFirstStart(false);
                BaseActionHelper.with(GuideWelcomeActivity.this).handleAction(ActionHeads.LMActionHome);
                finish();
            }
        });
    }

    public static Intent callIntent(Context context) {
        return new Intent(context, GuideWelcomeActivity.class);
    }
}
